//
//  ProgressView.m
//  DataManagerDemo
//
//  Created by yewei on 12-6-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ProgressView.h"

const float RECT_WIDTH = 120.0;
const float RECT_HEIGHT = 80.0;
const float RADIUS = 10.0;
const float FONT = 16.0;
const float INDICATOR_OFFSET = 15.0;
const float ALPHA = 0.6;
const float LABEL_HEIGHT = 60;

////////////////////////////////圆角矩形类/////////////////////////
@interface RoundRectView : UIView
{
    //  CGSize m_rectSize;
    
    float m_radius;
    
    float m_alpha;
    
    NSString *m_message;
    
    UIActivityIndicatorView *m_indicatorView;
    
}


-(id)initWithFrame:(CGRect)frame message:(NSString *)m textColor:(UIColor *)color;
//-(void)setSize:(CGSize)size radius:(float)r alpha:(float)alpha;
-(void)bulidIndicatorView;

@property (nonatomic) BOOL rectHidden;
@end


@implementation RoundRectView
@synthesize rectHidden;


-(void)dealloc
{
    [m_message release];
    m_message = nil;
    [m_indicatorView release];
    m_indicatorView = nil;
    [super dealloc];
}

-(id)initWithFrame:(CGRect)frame message:(NSString *)m textColor:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if(self != nil)
    {
        self.rectHidden = NO;
        m_message = [m copy];
        m_indicatorView = nil;
        m_radius = RADIUS;
        m_alpha = ALPHA;
        self.backgroundColor = [UIColor clearColor];
        
        //加载旋转视图
        [self bulidIndicatorView];
        
        if([m_message length] > 0)
        {
            //加载文字
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height - LABEL_HEIGHT , frame.size.width, LABEL_HEIGHT)];
            label.text = m;
            label.font = [UIFont systemFontOfSize:FONT];
            if(nil != color)
            {
                label.textColor = color;
            }
#ifdef  __IPHONE_6_0
            label.textAlignment = NSTextAlignmentCenter;
#else
            label.textAlignment = UITextAlignmentCenter;
#endif
            label.backgroundColor = [UIColor clearColor];
            label.numberOfLines = 2;
            label.tag = 9999;
            [self addSubview:label];
            [label release];
        }
        
    }
    return self;
    
}

-(void)setRectHidden:(BOOL)hidden
{
    rectHidden = hidden;
    if(hidden)
    {
        m_indicatorView.color = [UIColor blackColor];
        UILabel *label = (UILabel *)[self viewWithTag:9999];
        label.hidden = YES;
    }
}

-(void)bulidIndicatorView
{
    if(m_indicatorView != nil)
    {
        [m_indicatorView removeFromSuperview];
        [m_indicatorView release];
    }
    m_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    CGPoint indecatorPoint;
    
    if([m_message length] == 0)
    {
        indecatorPoint = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
    }
    else
    {
        indecatorPoint = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0 - INDICATOR_OFFSET);
    }
    m_indicatorView.center = indecatorPoint;
    [m_indicatorView startAnimating];
    [self addSubview:m_indicatorView];
    
}
//-(void)setSize:(CGSize)size radius:(float)r alpha:(float)alpha
//{
//    CGRect newframe = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height);
//    self.frame = newframe;
//    [self bulidIndicatorView];
//    m_radius = r;
//    if(alpha >= 0.0 && alpha <= 1.0)
//    {
//        m_alpha = alpha;
//    }
//
//}


-(void)drawRect:(CGRect)rect
{
    //画圆角矩形
    if(self.rectHidden)
    {
        return;
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // If you were making this as a routine, you would probably accept a rectangle
    // that defines its bounds, and a radius reflecting the "rounded-ness" of the rectangle.
    
    CGRect rrect = CGRectMake(0, 0, RECT_WIDTH, RECT_HEIGHT);//self.frame;
    CGFloat radius = m_radius;
    
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
    
    // Start at 1
    CGContextMoveToPoint(context, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetAlpha(context, m_alpha);
    CGContextSetLineWidth(context, 0);
    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
    
}

@end
////////////////////////////////////////////////////////////




/////////////////////////////父视图类//////////////////////////
@implementation ProgressView
@synthesize roundRectHidden;


-(id)initWithMessage:(NSString *)message textColor:(UIColor *)color frame:(CGRect)f
{
    //    CGRect frame;
    //    UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];//[[UIDevice currentDevice] orientation];
    //    if((currentOrientation == UIInterfaceOrientationLandscapeLeft)
    //       || currentOrientation == UIInterfaceOrientationLandscapeRight)
    //    {
    //        frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    //    }
    //    else
    //    {
    //        frame = [UIScreen mainScreen].bounds;
    //    }
    self = [super initWithFrame:f];
    if(self != nil)
    {
        self.bCoverTouchEvent = YES;
        self.backgroundColor = [UIColor colorWithRed:125/255.0 green:125/255.0  blue:125/255.0  alpha:0.2];
        m_roundRectView = [[RoundRectView alloc]initWithFrame:CGRectMake((self.frame.size.width - RECT_WIDTH)/2.0, (self.frame.size.height - RECT_HEIGHT)/2.0, RECT_WIDTH, RECT_HEIGHT) message:message textColor:color];
        [m_roundRectView setNeedsDisplay];
        //  m_roundRectView.alpha = 0.4;
        // m_roundRectView.layer
        [self addSubview:m_roundRectView];
        
        
    }
    return self;
    
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(self.bCoverTouchEvent)
    {
        return YES;
    }
    return NO;
}

-(void)setRoundRectHidden:(BOOL)hidden
{
    m_roundRectView.rectHidden = hidden;
}

-(void)dealloc
{
    [m_roundRectView release];
    m_roundRectView = nil;
    [super dealloc];
}





@end
